package com.example.batterynotification;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class CustomSwitch extends Button {
	protected int onPic = -1;
	protected int offPic = -1;
	private int width = 20;
	private int height = 5;
	protected boolean checked = false;
	
	public CustomSwitch(Context context, AttributeSet attrs) {
		super(context, attrs);
		final Context viewContext = context;
		setClickable(true);
		Log.i("CustomSwitch", "in the constructor");
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.CustomSwitch, 0, 0);

		try {
			int temp = a.getResourceId(R.styleable.CustomSwitch_offPic, -1);
			if (temp != -1)
				setOffPic(temp);

			temp = a.getResourceId(R.styleable.CustomSwitch_onPic, -1);
			if (temp != -1)
				setOnPic(temp);

		} finally {
			a.recycle();
		}
		// setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// setChecked(!isChecked());
		// }
		// });

	}

	public int getOnPic() {
		return onPic;
	}

	public void setOnPic(int onPic) {
		this.onPic = onPic;
		invalidate();
		requestLayout();
	}

	public int getOffPic() {
		return offPic;
	}

	public void setOffPic(int offPic) {
		this.offPic = offPic;
		invalidate();
		requestLayout();
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
		invalidate();
		requestLayout();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("CustomSwitch", "ONDraw");
		int temp = (isChecked() ? getOnPic() : getOffPic());
		if (temp != -1) {
			Drawable d = getResources().getDrawable(temp);

			d.setBounds(0, 0, width, height);

			setBackground(d);
			// new Dimension(width, height)
			setMaxHeight(height);
			setMaxWidth(width);
			Log.i("CustomSwitch",
					"d=" + d.toString() + " canvas= " + canvas.toString());
		}
		super.onDraw(canvas);
	}

	@Override
	public boolean performClick() {
		setChecked(!isChecked());
		return super.performClick();
	}
}
